package maldade.configuration;

import maldade.persistence.configuration.PersistenceConfiguration;

import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

/**
 * Static configuration loader class.
 * 
 * @author rnojiri
 */
public class ConfigurationLoader 
{
	private static Logger log;
	
	private enum Properties
	{
		log4j_file_path,
		message_resource_file_path,
		hibernate_cfg_file_path,
		windows_live_delegated_auth_file
	}
	
	protected PropertiesConfiguration configuration;
	
	protected static ConfigurationLoader instance;
	
	/**
	 * Loads a configuration file from a given path.
	 * 
	 * @param filePath
	 * @throws Exception
	 */
	protected ConfigurationLoader(String filePath) throws Exception
	{
		if(filePath == null)
		{
			throw new Exception("File path configuration cannot be null.");
		}
		
		configuration = new PropertiesConfiguration(filePath);
		
		configuration.setAutoSave(false);
		
		configureLog4J();
		configurePersistence();
		configureMessageLoader();
	}
	
	/**
	 * Configures log4j.
	 */
	private void configureLog4J()
	{
		String log4JFilePath = getLog4JFilePath();
		
		DOMConfigurator.configure(log4JFilePath);
		
		log = Logger.getLogger(ConfigurationLoader.class);
		
		log.info("Log4J was configured with \"" + log4JFilePath + "\"");
	}
	
	/**
	 * Configures persistence.
	 */
	private void configurePersistence()
	{
		PersistenceConfiguration.configure(getHibernateCfgFilePath());
	}
	
	/**
	 * Configures log4j.
	 */
	private void configureMessageLoader()
	{
		String filePath = getMessageResourceFilePath();
		
		log.info("Configuring Message Loader with: " + filePath);
		
		try
		{
			MessageLoader.initialize(filePath);
		}
		catch(Exception e)
		{
			log.error(e.getMessage(), e);
			
			return;
		}
		
		log.info("Message Loader was configured.");
	}
	
	/**
	 * Initializes this instance.
	 * 
	 * @param filePath
	 * @throws Exception
	 */
	public static void initialize(String filePath) throws Exception
	{
		if(instance == null)
		{
			instance = new ConfigurationLoader(filePath);
			
			log.info("Servlet initialized!");
		}
	}
	
	/**
	 * Returns the instance.
	 * 
	 * @return ConfigurationLoader
	 */
	public static ConfigurationLoader getInstance()
	{
		return instance;
	}
	
	/**
	 * Logs the destruction of this servlet.
	 */
	public static void destroy()
	{
		log.info("Servlet destroyed!");
	}
	
	/**
	 * Returns the log4j configuration path.
	 * 
	 * @return String
	 */
	public String getLog4JFilePath()
	{
		return configuration.getString(Properties.log4j_file_path.toString());
	}
	
	/**
	 * Returns the hibernate configuration path.
	 * 
	 * @return String
	 */
	public String getHibernateCfgFilePath()
	{
		return configuration.getString(Properties.hibernate_cfg_file_path.toString());
	}
	
	/**
	 * Returns the message resource file path.
	 * 
	 * @return String
	 */
	public String getMessageResourceFilePath()
	{
		return configuration.getString(Properties.message_resource_file_path.toString());
	}
	
	/**
	 * Returns the windows live delegated authorization path.
	 * 
	 * @return String
	 */
	public String getWindowsLiveDelegatedAuthPath()
	{
		return configuration.getString(Properties.windows_live_delegated_auth_file.toString());
	}
}
